草庐IT

c++ - std::unique_ptr 和指向指针的指针

全部标签

go - 无法调用 unsafe.Pointer(指向 C 函数)作为函数

我正在尝试调用具有相同签名的C函数,它们采用2个int参数。并且这个错误cannotcallnon-functionf(typeunsafe.Pointer)在编译时出现。packagemain/*intadd(inta,intb){returna+b;}intsub(inta,intb){returna-b;}*/import"C"import("fmt""unsafe")funcmain(){a:=C.int(1)b:=C.int(2)fx:=make([]unsafe.Pointer,2)fx[0]=C.addfx[1]=C.subfor_,f:=rangefx{fmt.Prin

c - O_NONBLOCK SOCK_STREAM 限制为 8192

我正在从C程序写入SOCK_STREAM正在从go程序监听的Unix域套接字,使用net.Listen("unix",sockname).当我将套接字设置为O_NONBLOCK使用fcntl(),我看到C程序在第一次写入时只写入了8192字节。失败后,我监控并回写剩余数据,但我服务器上读取的数据在这种情况下是无效的。当我不使用O_NONBLOCK时,然后整个8762字节被写在一个单一的写入中,一切都按预期工作。C客户端套接字连接if((fd=socket(AF_UNIX,SOCK_STREAM,0))==-1){return;}intflags=fcntl(fd,F_GETFL,0);

c - 为什么我用cgo的时候报错 "undefined reference to ` add2”

目录如下:-包括测试.h-liblibmytest.so-源代码测试.gotest.go代码如下:packagemain/*#cgoCFLAGS:-I../include#cgoLDFLAGS:-L../lib-lmytest#include"Test.h"*/import"C"funcmain(){C.add2(10,10)}当我使用gobuildtest.go时,控制台报告:#command-line-arguments/tmp/go-build168903458/command-line-arguments/_obj/test.cgo2.o:在函数_cgo_9efddd4c1a4

pointers - Go函数指针问题

这个问题在这里已经有了答案:MyobjectisnotupdatedevenifIusethepointertoatypetoupdateit(3个答案)GolangOperatorOverloading(1个回答)Golangchangingvaluesofastructinsideamethodofanotherstruct(2个答案)CopyinstancesoftypeT,whenanyofthemethodsofanamedtypeThaveapointerreceiver(1个回答)关闭5年前。我有一个结构typekeeperstruct{ptrint32}然后我给它添加一

pointers - Go 中的指针解除引用是如何工作的?

我正在http://tour.golang.org/学习golang教程,并在example29中尝试了一些东西为了方便大家引用,原例子复制在这里:packagemainimport"fmt"typeVertexstruct{X,Yint}var(p=Vertex{1,2}//hastypeVertexq=&Vertex{1,2}//hastype*Vertexr=Vertex{X:1}//Y:0isimplicits=Vertex{}//X:0andY:0)funcmain(){fmt.Println(p,q,r,s)}它非常基础,展示了如何创建这个奇特的新结构Vertex的实例。E

golang 中的 C long double

我正在将算法从C移植到Go。我有点困惑。这是C函数:voidgauss_gen_cdf(uint64_tcdf[],longdoublesigma,intn){inti;longdoubles,d,e;//Calculations...for(i=1;i并在for循环中将值“s”分配给数组cdf中的元素“x”。这怎么可能?据我所知,longdouble是float64(在Go上下文中)。所以我不应该能够编译C代码,因为我正在将一个longdouble分配给一个只包含uint64元素的数组。但C代码运行良好。那么有人可以解释为什么这是有效的吗?非常感谢。更新:函数的原始C代码可以在这里找

pointers - 指向方法参数中接口(interface)的指针?

Gistwithcode如何在第30行使用接口(interface)Herbivore代替*Mouse?我想将实现Herbivore接口(interface)的不同结构传递给方法eatingVictim,而不仅仅是Mouse 最佳答案 让我解释一下:首先在这个函数中:func(predatorCat)eatingVictim(victim*Mouse){fmt.Println(predator.name+"'seatingvictim"+victim.name)predator.hungry=falsevictim.alive=fa

database - 如何使用没有循环指针的反射在 Golang 中调用 Scan variadic 函数?

我找到了关于howtocalltheScanvariadicfunctioninGolangusingreflection的答案。并且没有声望在那里问。这里是代码的主要部分:values:=make([]interface{},count)valuePtrs:=make([]interface{},count)forrows.Next(){fori,_:=rangecolumns{valuePtrs[i]=&values[i]}rows.Scan(valuePtrs...)...}而且我不明白为什么必须循环此语句?为什么for在forrows.Next中?forrows.Next(){

c - 如何从 Go 函数返回 C 指针?

我有疑问是否可以从C上的Go函数指针返回?例如main.c可以是:structopen_db_returndb_ptr=open_db(db_path);GoSlicebacket={"DB",2,2};GoSlicekey={"CONFIG",6,6};structget_value_returnval=get_value(db_ptr.r0,backet,key);close_db(db_ptr.r0);接下来是Go代码://exportopen_dbfuncopen_db(pathstring)(interface{},error){db,err:=db.Open(path,06

pointers - 在 Go 中使用类似的指针结构分配结构

我有两个相似的结构,我想将一个分配给另一个。第一个“Equipment”是用来匹配数据库的结构。第二个“JsonEquipment”是解析JSON数据的辅助结构。例子如下:typeEquipmentstruct{IDuintCategoryIDuintIpstringLoginstringPasswordstring}typeJsonEquipmentstruct{ID*uintCategory*stringIp*stringLogin*stringPassword*string}指针用于检查该字段是否存在于JSON中。更多信息:Howtorecognizevoidvalueandun